﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IllustratedCsharp7_ch20_Linq
{
    public class Student                //声明类
    {
        public int StID;
        public string LastName;
    }
    public class CourseStudent
    {
        public string CourseName;
        public int StID;
    }
    class chapt20_5JoinClause
    {
        static Student[] students = new Student[] {
            new Student{ StID = 1,LastName="Carson"},
            new Student{ StID = 2,LastName="Klassen"},
            new Student{ StID = 3,LastName="Fleming"},
        };

        static CourseStudent[] studentsInCourses = new CourseStudent[] {
            new CourseStudent{ CourseName="Philosophy",StID = 1},
            new CourseStudent{ CourseName="Philosophy",StID = 2},
            new CourseStudent{ CourseName="History",StID = 1},
            new CourseStudent{ CourseName="History",StID = 3},
            new CourseStudent{ CourseName="Physics",StID = 3},
        };

        static void _Main() {
            //查找所有选择了历史课的学生的姓氏
            var query = from s in students
                        join c in studentsInCourses on s.StID equals c.StID
                        where c.CourseName == "History"
                        select s.LastName;

            //显示所有选择了历史课的学生的名字
            foreach (var q in query)
                Console.WriteLine($"Student taking History:{q}");
        }
    }
}
